package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.service.PermissionService;
import com.atguigu.helper.PermissionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-31 17:45
 * @Description:
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    /**
     * 根据角色获取授权权限数据
     * @return
     */
    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
        //获取全部权限列表
        List<Permission> permissionList = permissionDao.findAll();

        //获取角色已经分配的权限数据
        List<Long> permissionIdList = rolePermissionDao.findPermissionIdListByRoleId(roleId);

        //构建ztree数据
        // { id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
        List<Map<String,Object>> zNodes = new ArrayList<>();
        for (Permission permission : permissionList) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",permission.getId());
            map.put("pId",permission.getParentId());
            map.put("name",permission.getName());
            if(permissionIdList.contains(permission.getId())){
                map.put("checked",true);
            }
            zNodes.add(map);
        }

        return zNodes;
    }

    /**
     * 保存角色权限
     * @param roleId
     * @param permissionIds
     * 删除原有的数据,把新的数据添加到数据库中
     */
    @Override
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {
        //删除
        rolePermissionDao.delete(roleId);
        
        //添加
        for (Long permissionId : permissionIds) {
            if(StringUtils.isEmpty(permissionId)) continue;
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionDao.insert(rolePermission);

        }
    }

    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {
        //根据admin账号id查询权限列表
        List<Permission> permissionList = null;
        if(adminId.longValue() == 1){
            //如果是超级管理员,默认分配所有权限
            permissionList = permissionDao.findAll();
        }else{
            permissionList = permissionDao.findListByAdminId(adminId);
        }
        //把权限数据构建成树形结构数据
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;
    }
}
